/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.d.lib.ui.view.sort;

import com.d.lib.ui.view.flowlayout.CommonHolder;
import java.util.ArrayList;
import java.util.List;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.app.Context;

public class SortLibAdapter<T> extends BaseItemProvider {

  protected Context mContext;
  protected List<T> mDataset;
  protected int mLayoutId;
  // protected MultiItemTypeSupport<T> mMultiItemTypeSupport; ## 需要第三方支持
  protected DataSetObserver mDataSetObserver;

  public SortLibAdapter(Context context, List<T> dataset, int layoutId) {
    mContext = context;
    mDataset = dataset != null ? new ArrayList<>(dataset) : new ArrayList<T>();
    mLayoutId = layoutId;
  }

  public void registerDataSetObserver(DataSetObserver observer) {
    this.mDataSetObserver = observer;
  }

  public void notifyDataSetChanged() {
    if (mDataSetObserver != null) {
      mDataSetObserver.notifyChanged();
    }
  }

  public List<T> getDataset() {
    return mDataset;
  }

  public void setDataset(List<T> dataset) {
    if (mDataset != null && dataset != null) {
      mDataset.clear();
      mDataset.addAll(dataset);
    }
  }

  public int getCount() {
    return mDataset != null ? mDataset.size() : 0;
  }

  public int getViewTypeCount() {
    return 1;
  }

  public int getItemViewType(int position) {
    return 0;
  }

  public T getItem(int position) {
    return mDataset != null && mDataset.size() > 0 ? mDataset.get(position) : null;
  }

  public long getItemId(int position) {
    return position;
  }

  @Override
  public Component getComponent(int position, Component convertView,
                                ComponentContainer parent) {
    final int itemViewType = getItemViewType(position);
    CommonHolder holder;
    if (convertView == null) {
      holder = onCreateViewHolder(parent, itemViewType);
    } else {
      // getTag
      holder = (CommonHolder) convertView.getTag();
      if (holder.getItemViewType() != itemViewType) {
        holder = onCreateViewHolder(parent, itemViewType);
      }
    }
    convert(position, holder, mDataset.get(position));
    return holder.itemView;
  }

  public CommonHolder onCreateViewHolder(ComponentContainer parent, int viewType) {
    int layoutId = mLayoutId;
    CommonHolder holder = CommonHolder.create(mContext, parent, layoutId);
    holder.setItemViewType(viewType);
    // setTag
    holder.itemView.setTag(holder);
    onViewHolderCreated(holder, holder.itemView);
    return holder;
  }

  public void onViewHolderCreated(CommonHolder holder, Component itemView) {

  }

  /**
   * @param position The position of the item within the adapter's data set.
   * @param holder   Holder
   * @param item     Data
   */
  public void convert(int position, CommonHolder holder, T item) {
  }

  ;

  public interface DataSetObserver {
    void notifyChanged();
  }
}
